package cn.sypt.yuhao.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.mail.MessagingException;
import javax.mail.Session;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.itcast.commons.CommonUtils;
import cn.itcast.mail.Mail;
import cn.itcast.mail.MailUtils;
import cn.itcast.servlet.BaseServlet;
import cn.sypt.yuhao.dao.UserDao;
import cn.sypt.yuhao.domain.User;

public class UserServlet extends BaseServlet {
	private UserDao ud = new UserDao();
	
	public void ajaxUser(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String uname=request.getParameter("uname");
		User user=ud.ajaxUser(uname);
		if(user==null){
			response.getWriter().print("1");
		}else{
			response.getWriter().print("该用户名已被注册，请重新输入！！");
		}
		
	}
	public void ajaxEmail(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String email=request.getParameter("email");
		User user=ud.ajaxEmail(email);
		if(user==null){
			response.getWriter().print("1");
		}else{
			response.getWriter().print("该邮箱已被注册，请重新输入！！");
		}
		
	}
	
	public void logout(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		
		request.getSession(true).removeValue("user");
		response.sendRedirect("index.jsp");
	}
	public String login(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String uname=request.getParameter("uname");
		System.out.println(uname);
		String upass=request.getParameter("upass");
		User user=ud.login(uname, upass);
		if(user==null){
			request.setAttribute("msg", "用户名或密码错误");
			return "f:/ui/login.jsp";
		}else {
			int level=ud.findBylevel(uname, upass);
			if(level==0){
				request.setAttribute("msg", "该用户未激活，请登录您的邮箱进行激活账户");
				return "f:/ui/login.jsp";
			}else if(level==1){
				request.getSession().setAttribute("user", user);
				return "f:/index.jsp";
			}else {
				request.setAttribute("msg", "对不起，您的账户已被拉入黑名单，请联系管理员");
				return "f:/ui/login.jsp";
			}
		}
	}
	public String regist(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		User user=CommonUtils.toBean(request.getParameterMap(), User.class);
		user.setUid(CommonUtils.uuid());
		String active=CommonUtils.uuid();
		user.setActive(active);
		/////
		
		
		if(ud.ajaxEmail(user.getUemail())!=null||ud.ajaxUser(user.getUemail())!=null){
			request.setAttribute("msg", "用户名或邮箱已被注册，请重新输入");
			return "f:/ui/zhuce.jsp";
		}
		
		String host = "smtp.163.com";//服务器主机名
		String name = "experimentlearning@163.com";//登录名
		String pass = "learning2016";//登录密码
		Session session = MailUtils.createSession(host, name, pass);
		
		/*
		 * 创建Mail对象
		 */
		String from = "experimentlearning@163.com";
		String to = user.getUemail();//发送对象，需要用户的邮箱....自己根据需要填写
		String subject ="来自高校实验学习平台的激活邮件";//邮件的主题....自己根据需要填写
		String content ="您好 "+user.getUname()+"请点击链接http://localhost:8080/sypt/UserServlet?method=active&active="+active+"    激活您的账号（请在48小时内激活该邮件，48小时之后请重新注册），如果通过点击以上链接无法访问，请将该网址复制并粘贴至新的浏览器窗口中，如果您错误收到了此邮件，无需执行任何操作来取消账户，次账户将不会被启动。如果您对账户有任何疑问，可以发送邮件至experimentlearning@163.com，我们将会及时解决您的疑问。";//邮件的内容....自己根据需要填写
		Mail mail = new Mail(from, to, subject, content);
		/*
		 * 发送邮件
		 */
		try {
			MailUtils.send(session, mail);
		} catch (MessagingException e) {
			throw new RuntimeException(e);
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
		
		///////
		String utime=getStringDate();
		user.setUtime(utime);
		ud.regist(user);
		request.setAttribute("msg", "恭喜您注册成功，请前往您的邮箱激活您的账户");
		return "f:/ui/msg.jsp";
	}

	public String active(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String active=request.getParameter("active");
		int level=(Integer) ud.findByActive(active);
		if(level==0){
			ud.active(active);
			request.setAttribute("msg", "恭喜您激活成功，请立即登录，享受在知识的海洋中遨游的快乐吧！！！");
		}else if(level==1){
			request.setAttribute("msg", "您已经激活过该账户，请不要重复激活");
		}else{
			request.setAttribute("msg", "对不起，该激活码是无效的激活码，请重新注册激活");
		}
		return "f:/ui/msg.jsp";
	}
	
	public static String getStringDate() {
		Date currentTime = new Date();
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String date = formatter.format(currentTime);
		return date;
	}
}
